package studio.raptor.databus.graph.rule;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Component;
import studio.raptor.databus.graph.common.DirectGraph;

/**
 * 接口对应事件的在内存中有向无环图
 *
 * @author jack
 * @since 1.0
 */
@Component
public class EventDirectGraph {

  //key:code 接口编码，value：接口中需要发送的事件的图结构
  private Map<String, DirectGraph> serviceGraph = new ConcurrentHashMap<>(50);


  public Map<String, DirectGraph> getServiceGraph() {
    return serviceGraph;
  }

  /**
   * 根据接口编码获取接口对应的有向无环图
   * @param code
   * @return 如果不存在，则返回一个空图
   */
  public DirectGraph getDirectGraph(String code) {
    if (serviceGraph.containsKey(code)) {
      return serviceGraph.get(code);
    }
    DirectGraph graph = new DirectGraph();
    serviceGraph.put(code, graph);
    graph.addRootNode();
    return graph;
  }

}
